package com.jiuwan.gamepass.auth.controller;

import com.jiuwan.gamepass.auth.pojo.AdminLoginCaptchaParam;
import com.jiuwan.gamepass.auth.pojo.AdminLoginParam;
import com.jiuwan.gamepass.dao.authDao.SysUserDao;
import com.jiuwan.gamepass.dto.authDto.Result;
import com.jiuwan.gamepass.pojo.authPojo.SysUser;
import com.jiuwan.gamepass.service.userImpl.AdminServiceImpl;
import com.jiuwan.gamepass.service.userImpl.UserDetailsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.security.Principal;
import java.util.HashMap;

/**
 * @version 1.0
 * 功能目的描述 ：用于    登录控制器     ========>>>>>>>>>>>
 * @author： pang-yun
 * @date： 2021-07-01 09:48
 */

@RestController
public class LoginController {

    @Autowired
    private SysUserDao sysUserDao;
    @Autowired
    private AdminServiceImpl adminService;

    //登录成功之后返回token
    @PostMapping("/login")
    public Result login(@RequestBody AdminLoginParam adminLoginParam, HttpServletRequest request) {
        return adminService.login(adminLoginParam.getUsername(), adminLoginParam.getPassword(), request);
    }

    //登录加强版    需要进行验证码 校验
    @PostMapping("/loginPlus")
    public Result loginPlus(@RequestBody AdminLoginCaptchaParam adminLoginCaptchaParam, HttpServletRequest request) {
        return adminService.loginCaptcha(adminLoginCaptchaParam.getUsername(),
                adminLoginCaptchaParam.getPassword(),
                adminLoginCaptchaParam.getCode(), request);
    }


    //注销，拿到注销200之后 前端直接将token凭证删除\
    @PostMapping("/logout")
    public Result logout() {
        Result ok = Result.ok();
        ok.setMessage("用户注销成功");
        return ok;
    }

    //获取当前登录用户对象
    @GetMapping("/admin/info")
    public Result getAdminInfo(Principal principal) {
        if (null == principal) {
            return null;
        }
        String name = principal.getName();
        SysUser sysUser = sysUserDao.selectByUsername(name);
        HashMap<String, Object> userMap = new HashMap<>();
        userMap.put("userInfo", sysUser);
        Result ok = Result.ok();
        ok.setData(userMap);
        return ok;

    }

}
